package com.lb.pdm.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lb.pdm.common.constants.RedisConstants;
import com.lb.pdm.common.result.GraceJSONResult;
import com.lb.pdm.entity.KeyPersonnel;
import com.lb.pdm.entity.PersonInfo;
import com.lb.pdm.entity.SysUser;
import com.lb.pdm.entity.bo.KeyPersonnelBO;
import com.lb.pdm.entity.vo.KeyPersonnelVO;
import com.lb.pdm.mapper.KeyPersonnelMapper;
import com.lb.pdm.mapper.PersonInfoMapper;
import com.lb.pdm.service.KeyPersonnelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 重点人口表 服务实现类
 * </p>
 *
 * @author pdm
 * @since 2025-10-29
 */
@Service
public class KeyPersonnelServiceImpl extends ServiceImpl<KeyPersonnelMapper, KeyPersonnel> implements KeyPersonnelService {
    @Autowired
    private PersonInfoMapper personInfoMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private KeyPersonnelMapper keyPersonnelMapper;

    @Override
    public GraceJSONResult inputKeyPersonnel(KeyPersonnelBO keyPersonnelBO, String token) {
        KeyPersonnel keyPersonnel = new KeyPersonnel();
        BeanUtils.copyProperties(keyPersonnelBO, keyPersonnel);

        String idCard = keyPersonnelBO.getIdCard();
        LambdaQueryWrapper<PersonInfo> personInfoQuery = new LambdaQueryWrapper<>();
        personInfoQuery.eq(PersonInfo::getIdCard, idCard);
        PersonInfo personInfo = personInfoMapper.selectOne(personInfoQuery);

        if (personInfo == null) {
            return GraceJSONResult.errorMsg("人员信息不存在");
        }

        String sysUserString = redisTemplate.opsForValue().get(RedisConstants.USER_TOKEN_PREFIX + token);
        SysUser sysUser = JSON.parseObject(sysUserString, SysUser.class);

        keyPersonnel.setPersonId(personInfo.getId());
        keyPersonnel.setMonitorStartDate(new Date());
        keyPersonnel.setStatus((byte)1);
        keyPersonnel.setCreateTime(new Date());
        keyPersonnel.setUpdateTime(new Date());
        keyPersonnel.setCreateBy(sysUser.getId());
        keyPersonnel.setUpdateBy(sysUser.getId());

        boolean result = this.save(keyPersonnel);

        if (!result) {
            return GraceJSONResult.errorMsg("录入失败");
        }

        return GraceJSONResult.ok();
    }

    @Override
    public List<KeyPersonnelVO> query(String name, String idCard, String category, Byte riskLevel, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        return keyPersonnelMapper.query(name, idCard, category, riskLevel, start, pageSize);
    }

    @Override
    public GraceJSONResult updateKeyPersonnel(Long id, Byte status, String token) {
        LambdaQueryWrapper<KeyPersonnel>  keyPersonnelQueryWrapper = new LambdaQueryWrapper<>();
        keyPersonnelQueryWrapper.eq(KeyPersonnel::getId, id);
        KeyPersonnel keyPersonnel = this.getOne(keyPersonnelQueryWrapper);
        if (keyPersonnel == null) {
            return GraceJSONResult.errorMsg("人员信息不存在");
        }

        keyPersonnel.setStatus(status);
        keyPersonnel.setUpdateTime(new Date());

        String sysUserString = redisTemplate.opsForValue().get(RedisConstants.USER_TOKEN_PREFIX + token);
        SysUser sysUser = JSON.parseObject(sysUserString, SysUser.class);
        keyPersonnel.setUpdateBy(sysUser.getId());
        keyPersonnel.setMonitorEndDate(new Date());

        boolean result = this.updateById(keyPersonnel);
        if (!result) {
            return GraceJSONResult.errorMsg("更新失败");
        }
        return GraceJSONResult.ok();
    }
}
